﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace aoprinter.Tool
{
    class GeometryTool
    {
        static public float LineSlope(PointF point1, PointF point2)
        {
            if (!point1.Equals(point2))
            {
                if (point1.X == point2.X)
                {
                    return float.PositiveInfinity;
                }
                else
                {
                    return (point1.Y - point2.Y) / (point1.X - point2.X);
                }
            }
            return float.NaN;
        }

        static public float LineIntercept(PointF point1, PointF point2)
        {
            float slope = LineSlope(point1, point2);
            return LineIntercept(slope, point1);
        }

        static public float LineIntercept(float slope, PointF point1)
        {
            if (float.IsNaN(slope))
            {
                return float.NaN;
            }
            if (float.IsInfinity(slope))
            {
                return float.PositiveInfinity;
            }
            return point1.Y - slope * point1.X;
        }

        static public float LineApeakSlope(float slope)
        {
            if (slope == 0)
            {
                return float.PositiveInfinity;
            }
            else if (float.IsInfinity(slope))
            {
                return 0;
            }
            else return -1 / slope;
        }

        static public PointF LineIntersection(float slope1, float intercept1, float slope2, float intercept2)
        {
            if (slope1 == slope2)
            {
                return PointF.Empty;
            }
            else
            {
                float x = (intercept2 - intercept1) / (slope1 - slope2);
                float y = slope1 * x + intercept1;
                return new PointF(x, y);
            }
        }

        static public PointF[] ParallelLine(PointF point1, PointF point2, float interSize)
        {
            float slope = LineSlope(point1, point2);
            float intercept = LineIntercept(slope, point2);
            if (float.IsNaN(slope))
            {
                return null;
            }
            if (float.IsInfinity(slope))
            {
                return new PointF[] { new PointF(point1.X + interSize, point1.Y), new PointF(point2.X + interSize, point2.Y) };
            }
            if (slope == 0)
            {
                return new PointF[] { new PointF(point1.X, point1.Y + interSize), new PointF(point2.X, point2.Y + interSize) };
            }
            float intercept2 = Math.Max(intercept - slope * interSize, intercept + slope * interSize);
            float interceptApeak = LineApeakSlope(slope);
            return new PointF[] { LineIntersection(slope, intercept2, interceptApeak, LineIntercept(interceptApeak, point1)),
                LineIntersection(slope, intercept2, interceptApeak, LineIntercept(interceptApeak, point2))};
        }
    }
}